import {
  resource
} from '../../../static/resource';
import {
  ds
} from '../../../tools/data';
import {
  ui
} from '../../../tools/ui';
import {
  shops
} from '../../../tools/shops';

Component({
  properties: {
    bottom: {
      type: String,
      value: '136rpx'
    },
    right: {
      type: String,
      value: '36rpx'
    },
  },
  data: {
    resource,
  },
  lifetimes: {
    ready() {}
  },
  methods: {
    async chatTap(e) {
      this.openMakePhone()
      this.triggerEvent('tap', e.detail)
    },
    async openMakePhone() {
      ui.loading()
      const actions = {}
      const ids = shops.getShopList().map(ele => {
        return ele.id
      })

      const userRes = await ds.coll('users')
        .where({
          role: 'master',
          shop: {
            id: ds.cmd.in(ids)
          }
        })
        .get()
      const users = userRes.data
      // console.log(users)
      users.forEach(user => {
        if (user.phoneNumber) {
          actions[user.phoneNumber] = user.shop.name
        }
      })

      ui.loading(false)

      ui.actionSheet(actions, (value, key) => {
        wx.makePhoneCall({
          phoneNumber: key,
        })
      })
    },
  }
})